package com.meij.activity;

import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;

import com.activity.BaseActivity;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;
import com.github.johnpersano.supertoasts.SuperActivityToast;
import com.github.johnpersano.supertoasts.SuperToast;
import com.github.johnpersano.supertoasts.util.Style;
import com.meij.bean.ErrorInfo;
import com.meij.http.Json;
import com.util.DebugLog;
import com.util.Info;

public class MjActivity extends BaseActivity implements AMapLocationListener {
	protected SuperActivityToast sat;
	private LocationManagerProxy locationManager;
	public Double longitude = 0.0, latitude = 0.0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 定位
		locationManager = LocationManagerProxy.getInstance(this);
		locationManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 5000, 10, this);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	/***
	 * get请求方式的公共参数拼接
	 * 
	 * @param url
	 * @return
	 */
	@Override
	public String getParams(String url, Context context) {
		if (url.contains("?")) {
			url = url + "&";
		} else {
			url = url + "?";
		}
		String client = "client=0";
		String uniquecode = "&imei=" + Info.getID(context);
		String version = "&version=" + Info.getVersionName(context);
		url = url + client + uniquecode + version;
		DebugLog.i("URL", url);
		return url;
	}

	/***
	 * post请求方式的公共参数拼接
	 */
	@Override
	public Map<String, Object> postParams(String url, Map<String, Object> params, Context context) {
		params.put("client", "0");
		params.put("imei", Info.getID(context));
		params.put("version", Info.getVersionName(context));
		DebugLog.i("URL", url);
		return params;
	}

	@Override
	public boolean hasError(JSONObject object) {
		ErrorInfo errorInfo = Json.getErrorInfo(object);
		String errorMsg = errorInfo.getErrormessage();
		if (!TextUtils.isEmpty(errorMsg)) {
			Toast(errorMsg);
			return true;
		} else {
			return false;
		}
	}

	/***
	 * 返回
	 */
	public void Back(View view) {
		finish();
	}

	/***
	 * 显示加载提示
	 */
	public void ShowPd(String msg) {
		if (sat == null || !sat.isShowing() && !isFinishing()) {
			sat = new SuperActivityToast(this, SuperToast.Type.PROGRESS);
			sat.setText(msg);
			sat.setIndeterminate(true);
			sat.setTextSize(SuperToast.TextSize.MEDIUM);
			sat.setAnimations(SuperToast.Animations.FADE);
			sat.setProgressIndeterminate(true);
			sat.show();
		}
	}

	/***
	 * 显示加载提示
	 */
	public void ShowPd() {
		ShowPd("加载中");
	}

	/***
	 * 隐藏提示
	 */
	public void DismissPd() {
		if (sat != null) {
			sat.dismiss();
		}
	}

	/***
	 * 提示
	 * 
	 * @param msg
	 */
	public void Toast(String msg) {
		SuperToast st = new SuperToast(this, Style.getStyle(Style.BLACK));
		st.setDuration(SuperToast.Duration.MEDIUM);
		st.setTextSize(SuperToast.TextSize.MEDIUM);
		st.setAnimations(SuperToast.Animations.FLYIN);
		st.setText(msg);
		st.show();
	}

	/***
	 * 提示
	 * 
	 * @param msg
	 */
	public void Toast(int msgId) {
		String msg = getResources().getString(msgId);
		Toast(msg);
	}

	@Override
	public void onLocationChanged(Location location) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onProviderDisabled(String provider) {

	}

	@Override
	public void onLocationChanged(AMapLocation location) {
		if (location != null) {
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			locationManager.destroy();
		}
	}
}
